package com.gimi.cloud.bbp.manage.service.mall.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.mall.MallAppBannerGroupDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.mall.MallAppBannerDTO;
import com.gimi.cloud.bbp.dto.mall.MallAppBannerGroupDTO;
import com.gimi.cloud.bbp.entity.mall.MallAppBannerGroupEntity;
import com.gimi.cloud.bbp.manage.service.mall.MallAppBannerGroupService;
import com.gimi.cloud.bbp.manage.service.mall.MallAppBannerService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * banner组相关接口实现类
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-01-30
 **/
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class MallAppBannerGroupServiceImpl implements MallAppBannerGroupService {
    private static final Logger logger = LoggerFactory.getLogger(MallAppBannerGroupServiceImpl.class);
    private MallAppBannerGroupDao mallAppBannerGroupDao;
    private MallAppBannerService mallAppBannerService;

    @Autowired
    public MallAppBannerGroupServiceImpl(MallAppBannerGroupDao mallAppBannerGroupDao, MallAppBannerService mallAppBannerService) {
        this.mallAppBannerGroupDao = mallAppBannerGroupDao;
        this.mallAppBannerService = mallAppBannerService;
    }

    @Override
    public List<MallAppBannerGroupDTO> queryList(MallAppBannerGroupDTO dto, boolean isPaging) throws Exception {
        if (isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT gro.* FROM mall_app_banner_group gro WHERE gro.is_delete = 0 AND gro.abg_name like '%'||:abgName||'%' AND gro.abg_status =:abgStatus  ORDER BY abg_status,abg_predict_start_time ";
        QueryParam param = new QueryParam();
        param.setSql(sql);
        param.setParamObject(dto);
        return mallAppBannerGroupDao.selectListDTOBySql(param);
    }

    @Override
    public MallAppBannerGroupDTO load(MallAppBannerGroupDTO dto, Object id) throws Exception {
        MallAppBannerGroupEntity sysRoleEntity = mallAppBannerGroupDao.queryBy("abg_id",id);
        if(null == sysRoleEntity){
            return null;
        }
        MallAppBannerGroupDTO bannerDTO = new MallAppBannerGroupDTO();
        ReflectUtils.copyPropertiesIgnoreNull(sysRoleEntity,bannerDTO);
        return bannerDTO;
    }

    @Override
    public MallAppBannerGroupDTO saveOrUpdate(MallAppBannerGroupDTO dto) throws Exception {
        MallAppBannerGroupEntity bannerGroup = null;
        if (null != dto.getAbgId()) {
            bannerGroup = mallAppBannerGroupDao.queryBy("abg_id", dto.getAbgId());
            if (null == bannerGroup) {
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取Bannerid为" + dto.getAbgId() + "的Banner信息，保存失败");
            }
        } else {
            bannerGroup = new MallAppBannerGroupEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNull(dto, bannerGroup);
        if (null != dto.getAbgId()) {
            mallAppBannerGroupDao.baseUpdateByPrimaryKey(bannerGroup);
        } else {
            mallAppBannerGroupDao.baseInsert(bannerGroup);
        }
        if(null == bannerGroup.getAbgId()){
            throw new MsgException(ResponseCodeEnum.NO_DATA,"系统异常，保存失败");
        }
        List<Integer> bannerItem = new ArrayList<>();
        if(null != dto.getMallAppBanners() && dto.getMallAppBanners().size() > 0){
            for(MallAppBannerDTO banner:dto.getMallAppBanners()){
                banner.setAbgId(bannerGroup.getAbgId());
                banner.setUid(dto.getUid());
                banner.setUname(dto.getUname());
                mallAppBannerService.saveOrUpdate(banner);
                bannerItem.add(banner.getAbrId());
            }
        }
        MallAppBannerDTO banner = new MallAppBannerDTO();
        ReflectUtils.copyPropertiesIgnoreNullAndEmptyStr(dto,banner);
        mallAppBannerService.deleteBannerByIds(banner,bannerItem,bannerGroup.getAbgId());
        ReflectUtils.copyPropertiesIgnoreNull(bannerGroup, dto);
        return dto;
    }

    @Override
    public int delete(MallAppBannerGroupDTO dto, Object id, boolean realDel) throws Exception {
        MallAppBannerGroupEntity banner = mallAppBannerGroupDao.queryBy("abg_id", id);
        if (null == banner) {
            throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取Bannerid为" + id + "的Banner信息，删除失败");
        }
        //非物理删除
        if(!realDel){
            ReflectUtils.copyPropertiesIgnoreNull(dto,banner);
            banner.setIsDelete(1);
            return mallAppBannerGroupDao.baseUpdateByPrimaryKey(banner);
        }
        return mallAppBannerGroupDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<MallAppBannerGroupDTO> queryAppBannerGroupAndInitChild(MallAppBannerGroupDTO dto, boolean isPaging) throws Exception {
        if (isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT gro.* FROM mall_app_banner_group gro WHERE gro.is_delete = 0 AND gro.abg_name like '%'||:abgName||'%' AND gro.abg_status =:abgStatus  ORDER BY abg_status,abg_predict_start_time ";
        QueryParam param = new QueryParam();
        param.setSql(sql);
        param.setParamObject(dto);
        List<MallAppBannerGroupDTO> mallAppBannerGroupList =  mallAppBannerGroupDao.selectListDTOBySql(param);
        if(null != mallAppBannerGroupList && mallAppBannerGroupList.size() > 0){
            MallAppBannerDTO appBanner = new MallAppBannerDTO();
            for(MallAppBannerGroupDTO mallAppBannerGroup:mallAppBannerGroupList){
                appBanner.setAbgId(mallAppBannerGroup.getAbgId());
                mallAppBannerGroup.setMallAppBanners(mallAppBannerService.queryList(appBanner,false));
            }
        }
        return mallAppBannerGroupList;
    }
}
